package org.speed.mes.base.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.speed.mes.base.domain.vo.BsMaterialTypeVo;
import org.speed.mes.base.domain.vo.BsMaterialVo;
import org.speed.mes.base.domain.vo.BsProcessRouteVo;
import org.speed.mes.base.domain.vo.BsProductTypeVo;
import org.speed.mes.base.domain.vo.BsUnitMeasureVo;
import org.speed.mes.base.service.IBsProcessRouteService;
import org.speed.mes.base.service.IBsProductTypeService;
import org.speed.mes.base.service.IBsUnitMeasureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.speed.mes.base.domain.bo.BsProductBo;
import org.speed.mes.base.domain.vo.BsProductVo;
import org.speed.mes.base.domain.BsProduct;
import org.speed.mes.base.mapper.BsProductMapper;
import org.speed.mes.base.service.IBsProductService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 产品Service业务层处理
 *
 * @author speed-mes
 * @date 2024-06-19
 */
@RequiredArgsConstructor
@Service
public class BsProductServiceImpl implements IBsProductService {

    private final BsProductMapper baseMapper;
    @Autowired
    private IBsProductTypeService productTypeService;
    @Autowired
    private IBsUnitMeasureService unitMeasureService;
    @Autowired
    private IBsProcessRouteService processRouteService;

    /**
     * 查询产品
     */
    @Override
    public BsProductVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询产品列表
     */
    @Override
    public TableDataInfo<BsProductVo> queryPageList(BsProductBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BsProduct> lqw = buildQueryWrapper(bo);
        Page<BsProductVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        List<BsProductVo> records = result.getRecords();
        records.stream().forEach(item -> {
            BsProductTypeVo productType = productTypeService.queryById(item.getTypeId());
            item.setTypeName(productType.getName());
            BsUnitMeasureVo unitMeasure = unitMeasureService.queryById(Long.parseLong(item.getUnit()));
            item.setUnitName(unitMeasure.getName());
            BsProcessRouteVo processRoute = processRouteService.queryById(item.getProcessRouteId());
            item.setProcessRouteName(processRoute.getName());
        });
        return TableDataInfo.build(result);
    }

    /**
     * 查询产品列表
     */
    @Override
    public List<BsProductVo> queryList(BsProductBo bo) {
        LambdaQueryWrapper<BsProduct> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BsProduct> buildQueryWrapper(BsProductBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BsProduct> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getCode()), BsProduct::getCode, bo.getCode());
        lqw.like(StringUtils.isNotBlank(bo.getName()), BsProduct::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getModel()), BsProduct::getModel, bo.getModel());
        lqw.eq(StringUtils.isNotBlank(bo.getUnit()), BsProduct::getUnit, bo.getUnit());
        lqw.eq(bo.getCapacity() != null, BsProduct::getCapacity, bo.getCapacity());
        lqw.eq(bo.getTypeId() != null, BsProduct::getTypeId, bo.getTypeId());
        lqw.eq(bo.getProcessRouteId() != null, BsProduct::getProcessRouteId, bo.getProcessRouteId());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), BsProduct::getStatus, bo.getStatus());
        lqw.eq(bo.getPackNumber() != null, BsProduct::getPackNumber, bo.getPackNumber());
        lqw.eq(bo.getSort() != null, BsProduct::getSort, bo.getSort());
        return lqw;
    }

    /**
     * 新增产品
     */
    @Override
    public Boolean insertByBo(BsProductBo bo) {
        BsProduct add = MapstructUtils.convert(bo, BsProduct.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改产品
     */
    @Override
    public Boolean updateByBo(BsProductBo bo) {
        BsProduct update = MapstructUtils.convert(bo, BsProduct.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(BsProduct entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除产品
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
